package com.ruoyi.tcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.DeletedEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.tcp.domain.AddressUnit;
import com.ruoyi.tcp.domain.Surveillance;
import com.ruoyi.tcp.mapper.SurveillanceMapper;
import com.ruoyi.tcp.service.IAddressUnitService;
import com.ruoyi.tcp.service.ISurveillanceService;
import jakarta.validation.Validator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * AI摄像头Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-20
 */
@Service
@Slf4j
public class SurveillanceServiceImpl extends ServiceImpl<SurveillanceMapper, Surveillance> implements ISurveillanceService {
    @Autowired
    private SurveillanceMapper surveillanceMapper;

    @Autowired
    protected Validator validator;
    @Autowired
    protected IAddressUnitService addressUnitService;
    ;

    @Override
    public Page<Surveillance> pageList(Page<Surveillance> page, Surveillance surveillance) {
        LambdaQueryWrapper<Surveillance> queryWrapper = getQueryWrapper(surveillance);
        return page(page, queryWrapper);
    }

    @Override
    public Surveillance getByMonitorCode(String monitorCode) {
        Wrapper<Surveillance> query = Wrappers.lambdaQuery(Surveillance.class)
                .eq(Surveillance::getDeviceNumber, monitorCode)
                .eq(Surveillance::getDeleted, DeletedEnum.NOMAL.getValue());
        return getOne(query, false);
    }

    @Override
    public List<Surveillance> listAll(Surveillance surveillance) {
        return list(getQueryWrapper(surveillance));
    }

    private LambdaQueryWrapper<Surveillance> getQueryWrapper(Surveillance surveillance) {
        LambdaQueryWrapper<Surveillance> queryWrapper = Wrappers.lambdaQuery(Surveillance.class);
        queryWrapper.eq(Surveillance::getDeleted, DeletedEnum.NOMAL.getValue());

//        设备名称
        if (StringUtils.isNotEmpty(surveillance.getDeviceName())) {
            queryWrapper.like(Surveillance::getDeviceName, surveillance.getDeviceName());
        }
        if (StringUtils.isNotEmpty(surveillance.getStreetName())) {
            queryWrapper.like(Surveillance::getStreetName, surveillance.getStreetName());
        }


//        设备序列号
        if (StringUtils.isNotEmpty(surveillance.getSerialNumber())) {
            queryWrapper.like(Surveillance::getSerialNumber, surveillance.getSerialNumber());
        }


//        设备编号
        if (StringUtils.isNotEmpty(surveillance.getDeviceNumber())) {
            queryWrapper.like(Surveillance::getDeviceNumber, surveillance.getDeviceNumber());
        }


//        省份名称
        if (StringUtils.isNotEmpty(surveillance.getProviceName())) {
            queryWrapper.like(Surveillance::getProviceName, surveillance.getProviceName());
        }


//        省份编码
        if (StringUtils.isNotEmpty(surveillance.getProviceCode())) {
            queryWrapper.eq(Surveillance::getProviceCode, surveillance.getProviceCode());
        }


//        市名称
        if (StringUtils.isNotEmpty(surveillance.getCityName())) {
            queryWrapper.like(Surveillance::getCityName, surveillance.getCityName());
        }


//        市编号
        if (StringUtils.isNotEmpty(surveillance.getCityCode())) {
            queryWrapper.eq(Surveillance::getCityCode, surveillance.getCityCode());
        }


//        区名称
        if (StringUtils.isNotEmpty(surveillance.getAreaName())) {
            queryWrapper.like(Surveillance::getAreaName, surveillance.getAreaName());
        }


//        区编码
        if (StringUtils.isNotEmpty(surveillance.getAreaCode())) {
            queryWrapper.eq(Surveillance::getAreaCode, surveillance.getAreaCode());
        }


//        地址
        if (StringUtils.isNotEmpty(surveillance.getAddress())) {
            queryWrapper.like(Surveillance::getAddress, surveillance.getAddress());
        }


//        社区
        if (StringUtils.isNotEmpty(surveillance.getCommunity())) {
            queryWrapper.like(Surveillance::getCommunity, surveillance.getCommunity());
        }


//        小区名称
        if (StringUtils.isNotEmpty(surveillance.getResidentialName())) {
            queryWrapper.like(Surveillance::getResidentialName, surveillance.getResidentialName());
        }


//        ip地址
        if (StringUtils.isNotEmpty(surveillance.getIp())) {
            queryWrapper.eq(Surveillance::getIp, surveillance.getIp());
        }


//        物理地址
        if (StringUtils.isNotEmpty(surveillance.getMacAddress())) {
            queryWrapper.eq(Surveillance::getMacAddress, surveillance.getMacAddress());
        }


//        设备品牌
        if (StringUtils.isNotEmpty(surveillance.getDeviceBrand())) {
            queryWrapper.like(Surveillance::getDeviceBrand, surveillance.getDeviceBrand());
        }


//        设备型号
        if (StringUtils.isNotEmpty(surveillance.getDeviceModel())) {
            queryWrapper.like(Surveillance::getDeviceModel, surveillance.getDeviceModel());
        }


//        设备状态 1 在线 0 离线
        if (Objects.nonNull(surveillance.getDeviceStatus())) {
            queryWrapper.eq(Surveillance::getDeviceStatus, surveillance.getDeviceStatus());
        }


//        删除标识 0正常 1已删除
        if (Objects.nonNull(surveillance.getDeleted())) {
            queryWrapper.eq(Surveillance::getDeleted, surveillance.getDeleted());
        }


        return queryWrapper;
    }
}
